# -*- coding:utf-8 -*- 
from google.appengine.ext import webapp
from model import *
from mywork import * 
from google.appengine.ext import db
import md5
import urllib
from google.appengine.api import images
import urllib2
import logging
from google.appengine.api import memcache
import string
class img(webapp.RequestHandler):
    def get(self):   
        self.name=urllib.unquote(self.request.path.split('/')[-1])
	logging.info('-----------------------------------------------------------------------')
	logging.info(self.name)
        img=ablum.all().filter('name =', str(self.name)).get()
	logging.info(str(img))
        #img=ablum.all().filter('title =', 'tyjty').fetch(limit=10)
        self.width=int(self.request.get('w') or 0)
        self.height=int(self.request.get('h') or 0)
	logging.info('-----------------------------------------------------------------------')
	#logging.info(img.name)
        try:
            img.name
	    logging.info(img.name)
            pass
        except:
            img=ablum.all().filter('name =', '88f5db500777fad5cc4770da32c0657f').get()
        if self.width and self.height:
            photo = images.Image(img.blob)
            photo.resize(width=self.width, height=self.height)
            photo.im_feeling_lucky()
            thumbnail = photo.execute_transforms(output_encoding=images.PNG)
            self.response.headers['Content-Type'] = "image/png"
            self.response.out.write(thumbnail)   
        else: 
            self.response.headers['Content-Type'] = "image/png"
            self.response.out.write(img.blob)#db.Blob(img))
        #self.response.out.write(self.name)
        #self.response.out.write(str(type(img)))
    def post(self):
        self.name=self.request.path.split('/')[-1]
        if self.name == 'img' or self.name =='':
            self.name=md5.md5(self.request.get('img')).hexdigest()
        else:
            pass
	if self.request.get('img_url') == 'http://':
		img_blob      =self.request.get('img')
	else:
		#import Image
		#import StringIO
		#headers={
#'User-Agent':'Mozilla/5.0 Firefox/3.6.8',
#}
		self.img_url  =self.request.get('img_url')
		#req = urllib2.Request(self.img_url,headers=headers)
		blobimg=urllib2.urlopen(self.img_url).read()
		img_blob      =db.Blob(blobimg)
        #img_blob      =self.request.get('img')
        img_descrip   =self.request.get('descrip')
        img_title     =self.request.get('title')
        img_name      =self.name
        img=ablum()
        img.name      =code_data(img_name)
        img.title     =code_data(img_title)
        img.blob      =img_blob#db.Blob(img_blod)
        img.descrip   =code_data(img_descrip)
        img.put()
        self.response.out.write('http://a-ablum.appspot.com/img/%s' %(self.name))
        self.redirect('http://a-ablum.appspot.com/img/%s' %(self.name))
    def delete(self):
        self.name=self.request.path.split('/')[-1]
        img=ablum.all().filter('name =', self.name).get()
        img.delete()
        self.response.out.write('delete ok')
    def put(self):
        self.name=self.request.path.split('/')[-1]
        img=ablum.all().filter('name =', self.name).get()
        img_blob      =self.request.get('img')
        img_descrip   =self.request.get('descrip')
        img_title     =self.request.get('title')
        img_name      =self.name
        img.name      =code_data(img_name)
        img.title     =code_data(img_title)
        img.blob      =db.Blob(img_blob)
        img.descrip   =code_data(img_descrip)
        img.put()
        self.response.out.write('update ok')
        
class upimg(webapp.RequestHandler):
    def get(self):  
        self.response.out.write(render('upimg.html',{}))


class quickimg(webapp.RequestHandler):
    def get(self):   
        self.name=urllib.unquote(self.request.path.split('/')[-1])
	logging.info('-----------------------------------------------------------------------')
	logging.info(self.name)
	img=get_obj(self.name)
	self.width=int(self.request.get('w') or 0)
        self.height=int(self.request.get('h') or 0)
	logging.info('-----------------------------------------------------------------------')
	if self.width and self.height:
            photo = images.Image(img)
            photo.resize(width=self.width, height=self.height)
            photo.im_feeling_lucky()
            thumbnail = photo.execute_transforms(output_encoding=images.PNG)
            self.response.headers['Content-Type'] = "image/png"
            self.response.out.write(thumbnail)   
        else: 
            self.response.headers['Content-Type'] = "image/png"
            self.response.out.write(img)#db.Blob(img))
    def post(self):
        self.name=self.request.path.split('/')[-1]
        if self.name == 'img' or self.name =='':
            self.name=md5.md5(self.request.get('img')).hexdigest()
        else:
            self.name=md5.md5(self.request.get('img')).hexdigest()
	if self.request.get('img_url') == 'http://':
		img_blob      =self.request.get('img')
	else:
		#import Image
		#import StringIO
		#headers={
#'User-Agent':'Mozilla/5.0 Firefox/3.6.8',
#}
		self.img_url  =self.request.get('img_url')
		#req = urllib2.Request(self.img_url,headers=headers)
		blobimg=urllib2.urlopen(self.img_url).read()
		img_blob      =db.Blob(blobimg)
        #img_blob      =self.request.get('img')
        img_descrip   =self.request.get('descrip')
        img_title     =self.request.get('title')
        img_name      =self.name
        img=ablum()
        img.name      =code_data(img_name)
        img.title     =code_data(img_title)
        img.blob      =img_blob#db.Blob(img_blod)
        img.descrip   =code_data(img_descrip)
        img.put()
        self.response.out.write('http://a-ablum.appspot.com/img/%s' %(self.name))
        self.redirect('http://a-ablum.appspot.com/img/%s' %(self.name))
    def delete(self):
        self.name=self.request.path.split('/')[-1]
        img=ablum.all().filter('name =', self.name).get()
        img.delete()
        self.response.out.write('delete ok')
    def put(self):
        self.name=self.request.path.split('/')[-1]
        img=ablum.all().filter('name =', self.name).get()
        img_blob      =self.request.get('img')
        img_descrip   =self.request.get('descrip')
        img_title     =self.request.get('title')
        img_name      =self.name
        img.name      =code_data(img_name)
        img.title     =code_data(img_title)
        img.blob      =db.Blob(img_blob)
        img.descrip   =code_data(img_descrip)
        img.put()
        self.response.out.write('update ok')


def get_obj(key):
	logging.log(logging.INFO,"key=%s"%key)
	key=string.strip(key)
	#key_obj=db.Key(encoded=key) 
	obj=memcache.get(key)
	if obj is None:
	#obj=db.get(key_obj)
		obj=ablum.all().filter('name =', str(key)).get()
		if obj is None:
			obj=ablum.all().filter('name =', 'ea596a69a7f9bff207cbaee077d7fba4').get()
			memcache.set(key ,obj.blob)
			return obj.blob
		else:
			memcache.set(key ,obj.blob)
			return obj.blob
	else:
		return obj


